package com.study.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.study.constant.MessageConstant;
import com.study.dao.MemberDao;
import com.study.dao.OrderDao;
import com.study.dao.OrderSettingDao;
import com.study.entity.Result;
import com.study.pojo.Member;
import com.study.pojo.Order;
import com.study.pojo.OrderSetting;
import com.study.service.OrderService;
import com.study.utils.DateUtils;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = OrderService.class)
@Transactional
public class OrderServiceImpl implements OrderService {

    @Resource(name = "orderDao")
    private OrderDao orderDao;
    @Resource(name = "orderSettingDao")
    private OrderSettingDao orderSettingDao;
    @Resource(name = "memberDao")
    private MemberDao memberDao;

    @Override
    public Result submit(Map map) {
        try {
            String orderDate = (String) map.get("orderDate");
            Date date = DateUtils.parseString2Date(orderDate);
            OrderSetting orderSetting = orderSettingDao.findByOrderDate(date);
            if(orderSetting == null){
                return new Result(false,MessageConstant.SELECTED_DATE_CANNOT_ORDER);
            }

            Integer number = orderSetting.getNumber();
            Integer reservations = orderSetting.getReservations();
            if(reservations >= number){
                return new Result(false,MessageConstant.ORDER_FULL);
            }

            String telephone = (String) map.get("telephone");
            Member member = memberDao.findByTelephone(telephone);
            if(member != null){
                Integer memberId = member.getId();
                Integer setmealId = Integer.parseInt((String) map.get("setmealId"));
                Order order = new Order(memberId, date, null, null, setmealId);
                List<Order> orders =  orderDao.findByCondition(order);
                if(orders != null && orders.size()>0){
                    return new Result(false,MessageConstant.HAS_ORDERED);
                }
            }

            orderSetting.setReservations(orderSetting.getReservations() + 1);
            orderSettingDao.editReservationsByOrderDate(orderSetting);

            if(member == null){
                member = new Member();
                member.setName((String) map.get("name"));
                member.setPhoneNumber(telephone);
                member.setIdCard((String) map.get("idCard"));
                member.setSex((String) map.get("sex"));
                member.setRegTime(new Date());
                memberDao.add(member);
            }

            Order order = new Order(member.getId(), date, (String) map.get("orderType"), Order.ORDERSTATUS_NO, Integer.parseInt((String) map.get("setmealId")));
            orderDao.submit(order);
            return new Result(true, MessageConstant.ORDER_SUCCESS,order.getId());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ORDER_ERROR);
        }
    }

    @Override
    public Map<String,Object> findById(Integer id) {
        Map<String, Object> map = orderDao.findById(id);
        if(map != null){
            try {
                String orderDate = DateUtils.parseDate2String((Date) map.get("orderDate"));
                map.put("orderDate",orderDate);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return map;
    }
}
